package com.zy.mc_module.mvp.presenter;

import android.content.Context;
import android.content.Intent;

import com.zy.mc_module.base.App;
import com.zy.mc_module.common.Constant;
import com.zy.mc_module.mvp.base.BasePresenter;
import com.zy.mc_module.mvp.bean.BaseBean;
import com.zy.mc_module.mvp.bean.ProtocolBean;
import com.zy.mc_module.mvp.bean.TokenBean;
import com.zy.mc_module.mvp.bean.UserBean;
import com.zy.mc_module.mvp.contract.LoginContract;
import com.zy.mc_module.mvp.contract.LoginContract.View;
import com.zy.mc_module.mvp.model.GetCodeModel;
import com.zy.mc_module.mvp.model.LoginModel;
import com.zy.mc_module.utils.GsonUtil;
import com.zy.mc_module.utils.SPUtil;
import com.zy.mc_module.utils.StringsUtils;
import com.zy.mc_module.utils.ToastUtil;
import com.zy.mc_module.utils.retrofit.RxObserver;
import com.zy.mc_module.utils.retrofit.RxTransform;
import com.zy.mc_module.utils.retrofit.error.ThdException;

import okhttp3.ResponseBody;

/**
 * Created on 18/11/30
 */
public class LoginPresenter extends BasePresenter<View> implements LoginContract.Presenter {
    private LoginModel loginModel;
    private GetCodeModel getCodeMode;
    public static int IMG_CODE = 51;
    public static int SMS_CODE = 52;

    public LoginPresenter() {
        loginModel = new LoginModel();
        getCodeMode = new GetCodeModel();
    }

    public void getLoginCode(Context context, String tel, String verityCode) {
        if (!StringsUtils.isMobile(tel)) {
            return;
        }
        getCodeMode.getLoginCode(tel, verityCode).compose(RxTransform.schedule())
                .subscribe(new RxObserver<ResponseBody>(this, context, true) {
                    @Override
                    protected void onBaseNext(ResponseBody response) {
                        getView().onGetCodeSuccess(response.toString());
                    }

                    @Override
                    protected void onBaseError(ThdException e) {
                        getView().onError(e);
                        ToastUtil.show(e.message);
                    }
                });
    }

    public void getForgetSmsCode(Context context, String tel, String verityCode) {
        if (!StringsUtils.isMobile(tel)) {
            return;
        }
        getCodeMode.getForgetSmsCode(tel, verityCode).compose(RxTransform.schedule())
                .subscribe(new RxObserver<ResponseBody>(this, context, true) {
                    @Override
                    protected void onBaseNext(ResponseBody response) {
                        getView().onGetCodeSuccess(response.toString());
                    }

                    @Override
                    protected void onBaseError(ThdException e) {
                        getView().onError(e);
                        ToastUtil.show(e.message);
                    }
                });
    }
    public void getRegisterCode(Context context, String tel, String verityCode) {
        if (!StringsUtils.isMobile(tel)) {
            return;
        }
        getCodeMode.getRegisterCode(tel, verityCode).compose(RxTransform.schedule())
                .subscribe(new RxObserver<ResponseBody>(this, context, true) {
                    @Override
                    protected void onBaseNext(ResponseBody response) {
                        getView().onGetCodeSuccess(response.toString());
                    }

                    @Override
                    protected void onBaseError(ThdException e) {
                        getView().onError(e);
                        ToastUtil.show(e.message);
                    }
                });
    }
    /***
     * 获取验证码
     * type 1、登录 2、注册 3、重置密码 4、更换手机号
     */
    @Override
    public void getCode(Context context, String type, String tel) {
        if (!StringsUtils.isMobile(tel)) {
            return;
        }
        getCodeMode.getCode(type, tel).compose(RxTransform.schedule())
                .subscribe(new RxObserver<BaseBean>(this, context, true) {
                    @Override
                    protected void onBaseNext(BaseBean baseBean) {
                        getView().onGetCodeSuccess(baseBean.msg);
                    }

                    @Override
                    protected void onBaseError(ThdException e) {
                        getView().onError(e);
                        ToastUtil.show(e.message);
                    }
                });
    }

    /***
     * 校验验证码/短信登录
     */
    @Override
    public void checkLogin(Context context, String tel, String code, String openid, String uuid, String nickname, String
            headimgurl) {
        if (code.length() >= 4) {
            loginModel.checkLogin(tel, code, openid, uuid, nickname, headimgurl)
                    .compose(RxTransform.schedule())
                    .subscribe(new RxObserver<BaseBean<UserBean>>(this, context, "正在登录...") {
                        @Override
                        protected void onBaseNext(BaseBean<UserBean> beanBaseBean) {
                        }

                        @Override
                        protected void onBaseError(ThdException e) {
                            getView().onError(e);
                            ToastUtil.show(e.message);
                        }
                    });
        }
    }

    /***
     * 账号密码登录
     */
    @Override
    public void login(Context context, int type, String userName, String phone, String password) {
        //登录类型type 1 账号密码登录 2 短信登录
        if (type == 1) {
            if (StringsUtils.isEmpty(userName) && StringsUtils.isEmpty(phone)) {
                ToastUtil.showShort("用户名/手机号不能为空");
                return;
            }
            if (password.length() <= 0) {
                ToastUtil.showShort("密码不能为空");
                return;
            }
        } else {
            if (!StringsUtils.isMobile(phone)) {
                return;
            }
        }
        loginModel.login(type, phone, phone, password).compose(RxTransform.schedule())
                .subscribe(new RxObserver<TokenBean>(this, context, "正在登录...") {
                    @Override
                    protected void onBaseNext(TokenBean tokenBean) {
                        SPUtil.putString(Constant.USER_TOKEN_INFO, GsonUtil.gsonString(tokenBean));
                        getView().onLoginSuccess(null);
//                        loginModel.getUserInfo().compose(RxTransform.schedule())
//                                .subscribe(new RxObserver<UserBean>(LoginPresenter.this,context) {
//                                    @Override
//                                    protected void onBaseNext(UserBean userBean) {
//                                        getView().onLoginSuccess(userBean);
//                                    }
//
//                                    @Override
//                                    protected void onBaseError(ThdException e) {
//                                        getView().onError(e);
//                                        ToastUtil.show(e.message);
//                                    }
//                                });
                    }

                    @Override
                    protected void onBaseError(ThdException e) {
                        getView().onError(e);
                        ToastUtil.show(e.message);
                    }
                });
    }

    public void getImgCode(Context context, String tel) {
        loginModel.getImgCode(tel).compose(RxTransform.schedule())
                .subscribe(new RxObserver<ResponseBody>(this, context, true) {
                    @Override
                    protected void onBaseNext(ResponseBody bean) {
                        getView().onSuccess(bean, IMG_CODE);
                    }

                    @Override
                    protected void onBaseError(ThdException e) {
                        getView().onError(e);
                        ToastUtil.show(e.message);
                    }
                });
    }

    public void register(Context context, String phone, String registerCode) {
        loginModel.register(phone, registerCode).compose(RxTransform.schedule())
                .subscribe(new RxObserver<ResponseBody>(this, context) {
                    @Override
                    protected void onBaseNext(ResponseBody baseBean) {
                        getView().onSuccess(baseBean,9);
                    }

                    @Override
                    protected void onBaseError(ThdException e) {
                        getView().onError(e);
                        ToastUtil.show(e.message);
                    }
                });
    }

    /***
     * 校验验证码
     */
    @Override
    public void checkCode(Context context, String tel, String code) {
        loginModel.checkCode(tel, code).compose(RxTransform.schedule())
                .subscribe(new RxObserver<BaseBean>(this, context, true) {
                    @Override
                    protected void onBaseNext(BaseBean bean) {
                        getView().onSuccess((Boolean) bean.data);
                    }

                    @Override
                    protected void onBaseError(ThdException e) {
                        getView().onError(e);
                        ToastUtil.show(e.message);
                    }
                });
    }

    /***
     * 查询用户手机号是否注册
     */
    public void checkPhoneRegister(Context context, String phone) {
        if (!StringsUtils.isMobile(phone)) {
            return;
        }
        loginModel.checkPhoneRegister(phone).compose(RxTransform.schedule())
                .subscribe(new RxObserver<BaseBean>(this, context, true) {
                    @Override
                    protected void onBaseNext(BaseBean bean) {
                        getView().onSuccess(bean, 1);
                    }

                    @Override
                    protected void onBaseError(ThdException e) {
                        getView().onError(e);
                        ToastUtil.show(e.message);
                    }
                });
    }

    /***
     * 协议、规则文章查询
     */
    public void queryArticleProtocol(Context context, String articleProtocolType, String queryType) {
        if (true) return;
        loginModel.queryArticleProtocol(articleProtocolType, queryType).compose(RxTransform.schedule())
                .subscribe(new RxObserver<BaseBean<ProtocolBean>>(this, context) {
                    @Override
                    protected void onBaseNext(BaseBean<ProtocolBean> bean) {
                        getView().onSuccess(bean.data, 2);
                    }

                    @Override
                    protected void onBaseError(ThdException e) {
                        getView().onError(e);
                        ToastUtil.show(e.message);
                    }
                });
    }


    /***
     * 绑定银行卡
     */
    public void bindBank(Context context, String name, String openingBank, String tel, String code, String bankId, String
            bankNo) {
        loginModel.bindBank(name, openingBank, tel, code, bankId, bankNo)
                .compose(RxTransform.schedule())
                .subscribe(new RxObserver<BaseBean>(this, context, true) {
                    @Override
                    protected void onBaseNext(BaseBean baseBean) {
                        //绑定成功

                    }

                    @Override
                    protected void onBaseError(ThdException e) {
                        getView().onError(e);
                        ToastUtil.show(e.message);
                    }
                });
    }

}
